//
//  Utils.m
//  MeicaiCrm
//
//  Created by zhou jianbang on 2019/4/16.
//  Copyright © 2019 meicai. All rights reserved.
//

#import "Utils.h"
#import <MCFoundation/MCFoundation.h>

@implementation Utils

+(UIButton *)createBtn:(NSDictionary *)btnDict
{
    UIButton * button = [[UIButton alloc]init];
    [button setBackgroundColor:[UIColor mcColorWithHexString:[btnDict mcObjectForKey:@"backgroundColor"]]];
    [button setTitleColor:[UIColor mcColorWithHexString:[btnDict mcObjectForKey:@"textColor"]] forState:UIControlStateNormal];
    NSNumber * fontSize = [btnDict mcObjectForKey:@"fontSize"];
    NSInteger font = 14;
    if(fontSize) {
        font = fontSize.integerValue;
    }
    NSNumber * bold = [btnDict mcObjectForKey:@"bold"];
    button.titleLabel.font = bold ? [UIFont boldSystemFontOfSize:font] : [UIFont systemFontOfSize:font];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitle: [btnDict mcObjectForKey:@"name"] forState:UIControlStateNormal];
    return button;
}
+(NSTextAlignment)jsonToTextAlign:( NSString *)align
{
    NSTextAlignment textAlign = NSTextAlignmentCenter;
    if([align isEqualToString: @"left"]){
        textAlign = NSTextAlignmentLeft;
    }else if ([align isEqualToString: @"right"]){
        textAlign = NSTextAlignmentRight;
    }
    return textAlign;
}
+ (void)setBorderWithView:(UIView *)view top:(BOOL)top left:(BOOL)left bottom:(BOOL)bottom right:(BOOL)right borderColor:(UIColor *)color borderWidth:(CGFloat)width
{
    if (top) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, view.frame.size.width, width);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (left) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, width, view.frame.size.height);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (bottom) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, view.frame.size.height - width, view.frame.size.width, width);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (right) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(view.frame.size.width - width, 0, width, view.frame.size.height);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
}
+(NSDictionary *)stringToJson:(NSString *)jsonString
{
    if (!jsonString) return nil;
    
    id dict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
    if([dict isKindOfClass:[NSDictionary class]]){
        return (NSDictionary *)dict;
    }
    return  nil;
}
@end
